/**
 * @Author YuanYuan
 * @Date 2022/9/13
 * @Time 20:21
 */
class Counter1 {
    public int count = 0;
    synchronized public void increase() {
        count++;
    }
}
public class TestAdd {

    /**
     * 每个线程循环 1w 次，累加变量 count 的值，
     * count 默认值为 0，注意线程安全问题。
     * @return
    */

    public static void main(String[] args) throws InterruptedException {
        Counter1 counter1 = new Counter1();
        Thread thread = new Thread(()-> {
            for (int i = 0; i < 10000; i++) {
                counter1.increase();
            }
        });

        Thread thread1 = new Thread(()-> {
            for (int i = 0; i < 10000; i++) {
                counter1.increase();
            }
        });

        thread.start();
        thread1.start();

        thread.join();
        thread1.join();
        System.out.println(counter1.count);
    }
}
